package com.lixin.smartclassroom.ui.home.tab5.activity

import android.graphics.Bitmap
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.aboutUs
import com.lixin.smartclassroom.api.addclassnews
import com.lixin.smartclassroom.api.addliuyan
import com.lixin.smartclassroom.api.uploadFiles
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivityMessageBoardBinding
import com.lixin.smartclassroom.ui.home.adapter.selectImages
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.load
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.utils.NativeUtil
import com.luck.picture.lib.entity.LocalMedia
import java.io.File
import java.util.ArrayList

/**
 * Description 留言板
 * Author: lwc
 * Date:2023/12/9 9:25
 */
class MessageBoardAct : BaseActivity<ActivityMessageBoardBinding>() {
    override fun getViewBinding() = ActivityMessageBoardBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "留言板"

    override fun textMenuContent() = "历史留言"

    override fun onTextMenuListener() {
        start(MessageBoardHistoryAct::class.java)
    }

    private var localMediaList = ArrayList<LocalMedia>()
    private var hasSelectType = 1
    private var imgList = ArrayList<String>()
    private var files = ArrayList<File>()
    private var imgListUrl = ArrayList<String>()
    var size = 0

    override fun iniView() {

        getAboutData()

        binding.apply {
            binding.selectImageVideo.selectImages(
                localMediaList,
                this@MessageBoardAct,
                hasSelectType,
                9,
                1,
                true,
                onClick = { }) { it ->
                size = it.size
                imgList.clear()
                localMediaList.clear()
                localMediaList.addAll(it)
            }
        }
    }

    private fun getAboutData() {
        binding.apply {
            scopeNetLife {
                val baseBean = aboutUs()
                if (baseBean.result == "0") {
                    if (baseBean.gongzhonghao.isNullOrEmpty())
                        tvWx.text = "微信公众号  "
                    else
                        tvWx.text = "微信公众号  " + baseBean.gongzhonghao

                    if (baseBean.youxiang.isNullOrEmpty())
                        tvEmail.text = "官方邮箱  "
                    else
                        tvEmail.text = "官方邮箱  " + baseBean.youxiang
                }
            }
        }

    }


    override fun onClick() {
        binding.apply {
            btnSave.onClick {
                if (etContent.text().isEmpty()) {
                    toast("请输入留言内容")
                    return@onClick
                }


                for (i in 0 until localMediaList.size) {
                    val filePath: String = localMediaList[i].realPath
                    val bitmap: Bitmap =
                        NativeUtil.compressBitmap(this@MessageBoardAct, filePath)
                    val file: File = NativeUtil.bitmapToFile(
                        this@MessageBoardAct,
                        bitmap,
                        60,
                        System.currentTimeMillis().toString()
                    )
                    files.add(file)
                }
                if (files.size == 0) {
                    toast("请上传图片")
                    return@onClick
                }

                if (files.size > 0) {
                    scopeNetLife {
                        val baseBean = uploadFiles(files)
                        if (baseBean.result == "0") {
                            imgListUrl.addAll(baseBean.urls!!)
                            var imageUrl = ""
                            for (i in 0 until imgListUrl.size) {
                                if (i == 0) {
                                    imageUrl = imgListUrl[i]
                                } else {
                                    imageUrl += "|" + imgListUrl[i]
                                }
                            }
                            val baseBean = addliuyan(etContent.text(), imageUrl, etPhone.text())
                            if (baseBean.result == "0") {
                                saveSuccessDialog("发布成功")
                                finish()
                            } else {
                                toast(baseBean.resultNote.toString())
                            }
                        } else {
                            toast(baseBean.resultNote.toString())
                        }

                    }
                } else {
                    scopeNetLife {
                        val baseBean = addliuyan(etContent.text(), "", etPhone.text())
                        if (baseBean.result == "0") {
                            saveSuccessDialog("发布成功")
                            finish()
                        } else {
                            toast(baseBean.resultNote.toString())
                        }
                    }

                }

            }
        }
    }

}